package com.example.smartcity.ui.frg

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.example.smartcity.R
import com.example.smartcity.app.App
import com.example.smartcity.http.UserInfo
import com.example.smartcity.ui.act.LoginActivity
import com.example.smartcity.utils.CommonUtil
import com.example.smartcity.utils.CommonUtil.Companion.isLogin
import kotlinx.android.synthetic.main.fragment_main_tab5.*

/**
 * @Author: Tailyou(祝文飞)
 * @CreateDate: 2021/10/7 14:53
 * @Description: 个人中心
 */
class MainEnterFrg5 : Fragment(R.layout.fragment_main_tab5) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    override fun onResume() {
        super.onResume()
        if (isLogin()) {
            //未登录
            tvAccount.text = "登录/注册"
            tvLogout.visibility = View.INVISIBLE
        } else {
            //已登录
            loadUserInfo()
        }
    }

    private fun initView() {
        tvAccount.setOnClickListener {
            if (isLogin()) {
                val intent = Intent(context, LoginActivity::class.java)
                startActivity(intent)
            }
        }
        llUserInfo.setOnClickListener {
            if (CommonUtil.isLogin()) {
                val intent = Intent(context, LoginActivity::class.java)
                startActivity(intent)
            } else {
                //TODO 跳转至个人信息
            }
        }
        llOrder.setOnClickListener {
            if (CommonUtil.isLogin()) {
                val intent = Intent(context, LoginActivity::class.java)
                startActivity(intent)
            } else {
                //TODO 跳转至订单列表
            }
        }
        llFeedback.setOnClickListener {
            if (CommonUtil.isLogin()) {
                val intent = Intent(context, LoginActivity::class.java)
                startActivity(intent)
            } else {
                //TODO 跳转至修改密码
            }
        }
        llPwd.setOnClickListener {
            if (CommonUtil.isLogin()) {
                val intent = Intent(context, LoginActivity::class.java)
                startActivity(intent)
            } else {
                //TODO 跳转至意见反馈
            }
        }
        tvLogout.setOnClickListener {
            App.appConfig.edit().putString("token", "").commit()
            onResume()
        }
    }

    private fun loadUserInfo() {
        App.fetchData<UserInfo>(
            "/prod-api/api/common/user/getInfo",
            UserInfo::class.java
        ) {
            App.fetchRCImage(it.user.avatar, ivPortrait, 90, R.mipmap.ic_avatar)
            tvAccount.text = "${it.user.userName}"
            tvLogout.visibility = View.VISIBLE
        }
    }

}